// 商品列表组件
<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/mangerHome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
      <el-breadcrumb-item>分类参数</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区 -->
    <el-card>
      <!-- 搜索和添加商品区  gutter设置列间距 span设置列的大小-->
      <el-row :gutter="20">
        <!-- 搜索商品 -->
        <el-col :span="14">    <!-- 搜索商品 更新最新的列表 后台完成查找 -->
          <el-input placeholder="请输入内容" v-model="querInfo.query" clearable @clear="getGoodsList">
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList" ></el-button>
          </el-input>
        </el-col>
        <!-- 添加商品按钮 跳转到添加商品的组件-->
        <el-col :span="5">
          <el-button type="primary" @click="goAddGoodsPage">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 表格 商品列表 -->
      <el-table :data="goodsList" border stripe>
        <el-table-column type="index" label="#"></el-table-column> <!-- 索引列 -->
        <el-table-column label="商品名称" prop="goods_name"></el-table-column> 
        <el-table-column label="价格（元）" prop="goods_price" width="90px"></el-table-column> 
        <el-table-column label="重量（kg）" prop="goods_weight" width="90px"></el-table-column> 
        <el-table-column label="创建时间" prop="add_time" width="150px">
          <template slot-scope="scope">
           {{scope.row.add_time | dateFormat}}
          </template>
        </el-table-column> 
        <el-table-column label="操作" width="140px">
          <template slot-scope="scope">
<!-- 编辑 --><el-button type="primary" icon="el-icon-edit" size="mini" ></el-button>
<!-- 删除 --><el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteGoodsById(scope.row)"></el-button>
          </template>
        </el-table-column> 
      </el-table>
      <!-- 分页区 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
      :current-page="querInfo.pagenum" :page-sizes="[1, 2, 5, 10]" :page-size="querInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
      <!-- 添加商品弹出来的对话框 -->
      <el-dialog title="添加商品" :visible.sync="addDialogVisible" width="50%">
      <!-- 表单区 -->
        <el-form :model="addGoodsForm" :rules="addGoodsFormRules" ref="addGoodsFormRef" label-width="100px" class="demo-ruleForm">
          <el-form-item label="名称" prop="goods_name">
            <el-input v-model="addGoodsForm.goods_name"></el-input>
          </el-form-item>
          <el-form-item label="价格" prop="goods_price">
            <el-input v-model="addGoodsForm.goods_price"></el-input>
          </el-form-item>
          <el-form-item label="数量" prop="goods_number">
            <el-input v-model="addGoodsForm.goods_number"></el-input>
          </el-form-item>
          <el-form-item label="重量" prop="goods_weight">
            <el-input v-model="addGoodsForm.goods_weight"></el-input>
          </el-form-item>
          <el-form-item label="商品介绍">
            <el-input v-model="addGoodsForm.goods_introduce"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="addDialogVisible = false">取 消</el-button>
          <el-button type="primary" >确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      querInfo:{   //访问后端用户数据所传参数对象
        query: '',    //搜索用户的关键字
        pagenum: 1,  //当前所在页数
        pagesize: 10  //每页显示多少条数据
      },
      goodsList:[],   //保存后台访问到的商品数据列表
      total:0,     //保存后台总数居条数 
      addDialogVisible:false,    //添加商品对话框显示隐藏
      addGoodsForm:{    //添加商品表单绑定的对应数据
        goods_name:'',
        goods_cat:[],
        goods_price:0,
        goods_number:0,
        goods_weight:0,
        goods_introduce:'',
      },
      addGoodsFormRules:{   //添加商品表单验证规则
        goods_name:{

        },
      },
    }
  },
  created() {
    this.getGoodsList()
  },
  methods: {
    async getGoodsList(){   //通过分页信息获取对应的商品列表信息数据
      const {data:res} = await this.$http.get('goods',{params:this.querInfo})   //发起请求
      // console.log(res)
      if(res.meta.status !== 200){   //请求失败
        return this.$message.error('访问后台数据失败，无法获取对应的商品列表信息！')
      }
      // 请求成功
      this.$message.success('获取商品列表成功！')
      this.goodsList = res.data.goods
      this.total = res.data.total
      // console.log(this.goodsList,this.total)
    },
    handleSizeChange(newSize){   //监听当pagesize每页显示多少条数据改变时
      this.querInfo.pagesize = newSize  
      this.getGoodsList()  //重新发送请求
    },
    handleCurrentChange(newSize){  //监听当pagenum当前显示页数号改变时
      this.querInfo.pagenum = newSize  
      this.getGoodsList()  //重新发送请求
    },
    async deleteGoodsById(goods){    //通过id删除对应商品
      const confirmResult = await this.$confirm.confirm('此操作将永久删除“'+goods.goods_name+'”商品，是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        // center: true
      }).catch(err => err)
      if(confirmResult !== 'confirm'){
          return this.$message.info('已取消删除操作')
      }
      // 确认删除 发起请求
      const {data:res} = await this.$http.delete('goods/' + goods.goods_id)
      // console.log(res)
      if(res.meta.status !== 200){   //请求失败
        return this.$message.error('访问后台数据失败，无法进行删除操作！')
      }
      // 请求成功
      this.$message.success('删除成功！')
      this.getGoodsList()    //更新列表
    },
    goAddGoodsPage(){    //添加商品按钮响应
      this.$router.push('goods/add')
    }
  },
}
</script>

<style scoped>

</style>